<?php
use \Workerman\Autoloader;

require_once __DIR__ . '/../../../../../Workerman/Autoloader.php';
require_once __DIR__.'/../../../Config/Const.php';
Autoloader::setRootPath(__DIR__.'/../../..');

$page_groupId = GROUP_ADMIN_MNG;
$page_pageId = PAGE_ADMIN_LIST;

require "../framepages/moudle_base.php";

$mode = $_GET['mode'];
$result = 'normal';
if ($mode == 'delete') {
    $id = $_GET['id'];
    $res_admin_user_record = \Data\Tree_admin_user_DAO::getRecord($id);
    if ($res_admin_user_record) {
        \Data\Tree_admin_user_DAO::delete($res_admin_user_record);
        $result = 'succ_delete';
    } else {
        $result = 'err_delete';
    }
}

?>

<div class="main-content">
    <div class="search-well">
        <form class="form-inline" style="margin-top: 0px;">
            用户检索 &nbsp;<input class="input-xlarge form-control"
                              placeholder="用户昵称/备注" id="4sStoreName" type="text"> &nbsp;
            <button class="btn btn-default" type="button" id="searchButton">
                <i class="fa fa-search"></i> 查找
            </button>
        </form>
    </div>
    <br />

    <div>
        <table class="table" style="word-break:break-all">
            <tr>
                <th width="9%">用户名</th>
                <th width="12%">姓名</th>
                <th width="9%">操作</th>
                <th></th>
            </tr>

            <?php
                $needle = $_GET['needle'];
                $res_admin_user_list = \Data\Tree_admin_user_DAO::getListByNeedle($needle);
                foreach ($res_admin_user_list as $res_admin_user_record) {
                    $id = $res_admin_user_record->getID();
                    $name = $res_admin_user_record->getNAME();
                    echo "
                        <tr>
                            <td>$id</td>
                            <td>$name</td>                      
                            <td><button type='button' class='btn btn-danger' onclick='doEdit($id)' >编辑</button></td>
                            <td><button type='button' class='btn btn-danger' onclick='doDelete($id)' >删除</button></td>                            
                        </tr>
                    ";
                }
            ?>

        </table>
    </div>

    <?php
    require "../framepages/footer.php";
    ?>
</div>
<script src="../lib/bootstrap/js/bootstrap.js"></script>
<script>

    $('#searchButton').click(function() {
        needle = $('#4sStoreName').val();
        url = "?needle="+needle;
        window.location.href = url;
        }
    );

    function init() {
        var msg = "<?php echo $result ?>";
        if (msg == 'succ_delete') {
            alert('删除成功');
        } else if (msg == 'err_delete') {
            alert('删除失败');
        }
    }

    function doEdit(id) {
        url = "./admin_user_edit.php?id="+id;
        window.location.href = url;
    }

    function doDelete(id) {
        var r = confirm("确定要删除吗?");
        if (r) {
            url = "./admin_user_list.php?mode=delete&id="+id;
            window.location.href = url;
        } else {
            alert("删除失败");
        }

    }

</script>
</body>
</html>
